import os
import time

from selenium import webdriver
from utils.screen_recording import SeleniumVideoRecorder
from config.config import loadConfigure
from handle.handle_yaml import ui_yaml_handle
from handle.handle_path import CASE_FOR_YAML_DIR
from server.settings import settings
from uiselfexe.core import execute_demo
from utils.send_msg_to_dingtalk import TestCaseReporter


def forRun(driver, dic):
    for file in os.listdir(CASE_FOR_YAML_DIR):
        case = ui_yaml_handle(file)
        execute_demo(case, data_record=dic, driver=driver)
    driver.quit()



def uiCaseRun():
    config = loadConfigure()
    dic = dict()
    loadconf = loadConfigure()
    project_name = loadconf["config"]["project"]
    dic["project"] = project_name
    browser = webdriver
    if config["config"]["browser"] == "chrome":
        driver = browser.Chrome()
    elif config["config"]["browser"] == "firefox":
        driver = browser.Firefox()
    elif config["config"]["browser"] == "edge":
        driver = browser.Edge()
    else:
        raise Exception("当前浏览器驱动尚未定义")
    forRun(driver, dic)
    time.sleep(2)
    driver.quit()
    return dic


def runAndSendMsgToDingTalk():
    result = uiCaseRun()
    reporter = TestCaseReporter(settings.WEBHOOK, settings.SECRET)
    reporter.send_test_report(result, at_all=True)

